import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
export default ({ command, mode }) => {
  let base = './' // 當此時為本地開發環境時，base路徑默認為"./"。
  if (command !== 'serve') {
    if (mode === 'bfs') {
      base = ''
    }
    if (mode === 'boss') {
      base = ''
    }
  }

  const env = loadEnv(mode, process.cwd())
  const proxyPrefix = env.VITE_APP_PROXY_PREFIX

  return defineConfig({
    base: base || env.VITE_APP_BASE,
    plugins: [vue()],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src')
      }
    },

    build: {
      chunkSizeWarningLimit: 1500
      // rollupOptions: {
      //   output: {
      //     manualChunks(id) {
      //       if (id.includes('node_modules')) {
      //         return id.toString().split("node_modules/")[1].split("/")[0].toString();
      //       }
      //     }
      //   }
      // }
    },
    server: {
      host: '0.0.0.0',
      port: env.VITE_APP_PORT || process.env.port,
      proxy: {
        [proxyPrefix]: {
          target: env.VITE_APP_BASE_URL,
          changeOrigin: true,
          ws: false,
          toProxy: true,
          rewrite: (path) => {
            return path.replace(new RegExp(`^${proxyPrefix}`), '')
          }
        }
      }
    }
  })
}
